import 'package:flutter/material.dart';
import 'package:wheel_of_fortune/model/address_model.dart';
import 'package:wheel_of_fortune/model/user_address_model.dart';
import 'package:wheel_of_fortune/product/widgets/address_list.dart';
import 'package:wheel_of_fortune/provider/address_provider.dart';
import 'package:wheel_of_fortune/utils/utils.dart';
import 'package:wheel_of_fortune/widgets/customer_button.dart';
import 'package:wheel_of_fortune/widgets/provider_widget.dart';

import 'widgets/text_field.dart';

class AddAddressPage extends StatefulWidget {
  final UserAddressModel userAddressModel;
  AddAddressPage({this.userAddressModel, Key key}) : super(key: key);

  @override
  _AddAddressPageState createState() => _AddAddressPageState();
}

class _AddAddressPageState extends State<AddAddressPage> {
  RegionModel region;
  ProvinceModel province;
  CityModel city;
  String address = '';
  bool flag = true;
  @override
  void initState() {
    super.initState();
    if (widget.userAddressModel == null) {
    } else {
      address = widget.userAddressModel.address;
      province = ProvinceModel(
        provinceId: widget.userAddressModel.provinceId,
        provinceName: widget.userAddressModel.provinceName,
      );
      city = CityModel(
        cityMunicipalityId: widget.userAddressModel.cityMunicipalityId,
        cityName: widget.userAddressModel.cityName,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return ProviderWidget<AddressProvider>(
      model: AddressProvider(context),
      onModelReady: (p) => p.initData(),
      builder: (context, provider, c) {
        return GestureDetector(
          onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
          child: Container(
            color: whiteColor,
            height: 473,
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: EdgeInsets.all(20),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          Icons.add,
                          size: 20,
                          color: mainColor,
                        ),
                        SizedBox(
                          width: 15,
                        ),
                        Text(
                          'Add Address',
                          style: TextStyle(
                            fontFamily: 'Avenir Next LT Pro',
                            fontSize: 14.0,
                            color: mainColor,
                          ),
                        )
                      ],
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 10, 42, 0),
                    child: Text(
                      'Address',
                      style: TextStyle(
                        color: grey70Color,
                        fontSize: 12,
                        fontWeight: FontWeight.bold,
                        fontFamily: "Avenir Next LT Pro",
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 10, 42, 0),
                    child: InputText(
                      type: 0,
                      text: address,
                      flag: flag,
                      onChange: (text) {
                        setState(() {
                          address = text;
                          flag = false;
                        });
                      },
                    ),
                  ),
                  // Padding(
                  //   padding: EdgeInsets.fromLTRB(42, 20, 42, 0),
                  //   child: Text(
                  //     'Region',
                  //     style: TextStyle(
                  //       color: grey70Color,
                  //       fontSize: 12,
                  //       fontWeight: FontWeight.bold,
                  //       fontFamily: "Avenir Next LT Pro",
                  //     ),
                  //   ),
                  // ),
                  // Padding(
                  //   padding: EdgeInsets.fromLTRB(42, 10, 42, 0),
                  //   child: InkWell(
                  //     onTap: () async {
                  //       if (provider.regionItems.length > 0) {
                  //         RegionModel data = await showModalBottomSheet(
                  //           context: context,
                  //           enableDrag: true,
                  //           isScrollControlled: true,
                  //           isDismissible: true,
                  //           backgroundColor: Colors.transparent,
                  //           builder: (c) {
                  //             return AlertDialog(
                  //               titlePadding: EdgeInsets.all(5),
                  //               shape: RoundedRectangleBorder(
                  //                 side: BorderSide.none,
                  //                 borderRadius:
                  //                     BorderRadius.all(Radius.circular(13)),
                  //               ),
                  //               title: AddressList(
                  //                   provider.regions, provider.regionItems),
                  //             );
                  //           },
                  //         );
                  //         if (data != null && data != region) {
                  //           region = data;
                  //           setState(() {
                  //             province = null;
                  //             city = null;
                  //           });
                  //           provider.getProvinces(region.code);
                  //         }
                  //       }
                  //     },
                  //     child: InputText(
                  //       text: region == null ? 'Region' : region.regionName,
                  //       isEnable: provider.regionItems.length > 0,
                  //     ),
                  //   ),
                  // ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 20, 42, 0),
                    child: Text(
                      'Province',
                      style: TextStyle(
                        color: grey70Color,
                        fontSize: 12,
                        fontWeight: FontWeight.bold,
                        fontFamily: "Avenir Next LT Pro",
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 10, 42, 0),
                    child: InkWell(
                      onTap: () async {
                        if (provider.provinceItems.length > 0) {
                          ProvinceModel data = await showModalBottomSheet(
                            context: context,
                            enableDrag: true,
                            isScrollControlled: true,
                            isDismissible: true,
                            backgroundColor: Colors.transparent,
                            builder: (c) {
                              return AlertDialog(
                                titlePadding: EdgeInsets.all(5),
                                shape: RoundedRectangleBorder(
                                  side: BorderSide.none,
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(13)),
                                ),
                                title: AddressList(
                                    provider.provinces, provider.provinceItems),
                              );
                            },
                          );
                          if (data != null && data != province) {
                            province = data;
                            setState(() {
                              city = null;
                            });
                            provider.getCitys(province.provinceCode);
                          }
                        }
                      },
                      child: InputText(
                        text: province == null
                            ? 'Province'
                            : province.provinceName,
                        isEnable: provider.provinceItems.length > 0,
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 20, 42, 0),
                    child: Text(
                      'City',
                      style: TextStyle(
                        color: grey70Color,
                        fontSize: 12,
                        fontWeight: FontWeight.bold,
                        fontFamily: "Avenir Next LT Pro",
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 10, 42, 0),
                    child: InkWell(
                      onTap: () async {
                        if (provider.cityItems.length > 0) {
                          city = await showModalBottomSheet(
                            context: context,
                            enableDrag: true,
                            isScrollControlled: true,
                            isDismissible: true,
                            backgroundColor: Colors.transparent,
                            builder: (c) {
                              return AlertDialog(
                                titlePadding: EdgeInsets.all(5),
                                shape: RoundedRectangleBorder(
                                  side: BorderSide.none,
                                  borderRadius:
                                      BorderRadius.all(Radius.circular(13)),
                                ),
                                title: AddressList(
                                    provider.citys, provider.cityItems),
                              );
                            },
                          );
                          if (city != null) {
                            setState(() {});
                          }
                        }
                      },
                      child: InputText(
                        text: city == null ? 'City' : city.cityName,
                        isEnable: provider.cityItems.length > 0,
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(42, 50, 42, 0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        BgButton(
                          title: 'Save',
                          color: deepYellowColor,
                          onTap: (address == '' ||
                                  // region == null ||
                                  province == null ||
                                  city == null)
                              ? null
                              : () => Navigator.of(context).pop(
                                    [
                                      address,
                                      // region,
                                      province,
                                      city,
                                    ],
                                  ),
                        ),
                        BorderButton(
                          title: 'Cancel',
                          color: deepYellowColor,
                          onTap: () => Navigator.of(context).pop([]),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
